package com.youxue.teachin.set.child.xuekexueduan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.support.v4.util.Pair;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.rest.vo.ResponseModel;
import com.library.uiframework.mvp.BasePresenter;
import com.youxue.teachin.R;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.set.child.xuekexueduan.vo.ItemVO;
import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-07 17:13
 * @version: 9.1.0
 */
public class XuekeXueduanPresenter extends BasePresenter<IXuekeXueduanView> {
    Map<String, Integer> iconMap = new HashMap<String, Integer>();
    /**
     * 构造方法
     *
     * @param iXuekeXueduanView 绑定的视图对象
     */
    public XuekeXueduanPresenter(IXuekeXueduanView iXuekeXueduanView) {
        super(iXuekeXueduanView);
        iconMap.put("小学", R.drawable.xd_xiaoxue);
        iconMap.put("初中", R.drawable.xd_chuzhong);
        iconMap.put("高中", R.drawable.xd_gazhong);
        iconMap.put("其他", R.drawable.xd_qita);

        iconMap.put("语文", R.drawable.xd_yuwen);
        iconMap.put("数学", R.drawable.xd_shuxue);
        iconMap.put("英语", R.drawable.xd_yingyu);
        iconMap.put("物理", R.drawable.xd_wuli);
        iconMap.put("历史", R.drawable.xd_lishi);
        iconMap.put("化学", R.drawable.xd_huaxue);
        iconMap.put("生物", R.drawable.xd_shenwu);
        iconMap.put("政治", R.drawable.xd_zhenzhi);
        iconMap.put("地理", R.drawable.xd_dili);
        iconMap.put("科学", R.drawable.xd_kexue);
        iconMap.put("信息技术", R.drawable.xd_xinxijishu);
    }



    public void loadData() {
        Observable<List<ItemVO>> stages = getStudyStages();
        Observable<List<ItemVO>> subject = getStudySubject();
        Observable<Pair<List<ItemVO>, List<ItemVO>>> zip = stages.zipWith(subject,
            new Func2<List<ItemVO>, List<ItemVO>, Pair<List<ItemVO>, List<ItemVO>>>() {
                @Override
                public Pair<List<ItemVO>, List<ItemVO>> call(List<ItemVO> itemVOs, List<ItemVO> itemVOs2) {
                    Pair<List<ItemVO>, List<ItemVO>> pair = new Pair<List<ItemVO>, List<ItemVO>>(itemVOs, itemVOs2);
                    return pair;
                }
            }).compose(RxSchedulersHelper.<Pair<List<ItemVO>, List<ItemVO>>>io_main());

        execute(zip, new DefaultSubscriber<Pair<List<ItemVO>, List<ItemVO>>>() {
            @Override
            public void onNext(Pair<List<ItemVO>, List<ItemVO>> pair) {
                super.onNext(pair);
                mBindView.bindData(pair.first, pair.second);
            }
        });
    }

    /**
     * 学科
     *
     * @return
     */
    public Observable<List<ItemVO>> getStudyStages() {
        Class<IXuekeXueduanRepository> claxx = IXuekeXueduanRepository.class;
        return ApiServiceManager.getOrCreateApiService(claxx)
            .getStudyStages()
            .map(new Func1<ResponseModel<List<ItemVO>>, List<ItemVO>>() {
                @Override
                public List<ItemVO> call(ResponseModel<List<ItemVO>> listResponseModel) {
                    if (listResponseModel.data != null) {
                        for (ItemVO vo : listResponseModel.data) {
                            vo.mIconRes = iconMap.get(vo.name);
                            vo.type = ItemVO.TYPE_STAGES;
                        }
                    }
                    return listResponseModel.data;
                }
            }).subscribeOn(Schedulers.io());

    }

    /**
     * 学段
     *
     * @return
     */
    public Observable<List<ItemVO>> getStudySubject() {
        Class<IXuekeXueduanRepository> claxx = IXuekeXueduanRepository.class;
        return ApiServiceManager.getOrCreateApiService(claxx)
            .getStudySubject()
            .map(new Func1<ResponseModel<List<ItemVO>>, List<ItemVO>>() {
                @Override
                public List<ItemVO> call(ResponseModel<List<ItemVO>> listResponseModel) {
                    if (listResponseModel.data != null) {
                        for (ItemVO vo : listResponseModel.data) {
                            vo.mIconRes = iconMap.get(vo.name);
                            vo.type = ItemVO.TYPE_SUBJECT;
                        }
                    }
                    return listResponseModel.data;
                }
            }).subscribeOn(Schedulers.io());

    }

}
